#include "Control.h"

bool Control::HitTest(vec2 pt)
{
	int left = pos.x;
	int top = pos.y;
	int right = pos.x + size.x;
	int bottom = pos.y + size.y;

	if (left < pt.x && right > pt.x)
	{
		if (top < pt.y && bottom > pt.y)
		{
			return true;
		}
	}
	return false;
}

bool Control::SetAttribute(const char* attrName, const char* attrVal)
{
	if (strcmp(attrName, "pos") == 0)
	{
		int x, y;
		sscanf_s(attrVal, "%d,%d", &x, &y);
		pos.x = x;
		pos.y = y;
		return true;
	}else if (strcmp(attrName, "size") == 0)
	{
		int w, h;
		sscanf_s(attrVal, "%d,%d", &w, &h);
		size.x = w;
		size.y = h;
		return true;
	}
	else if (strcmp(attrName, "margin") == 0)
	{
		int l, t, r, b;
		sscanf_s(attrVal, "%d,%d", &l, &t, &r, &b);
		margin.x = l;
		margin.y = t;
		margin.z = r;
		margin.w = b;
		return true;
	}
	else if (strcmp(attrName, "border") == 0)
	{
		int l, t, r, b;
		sscanf_s(attrVal, "%d,%d", &l, &t, &r, &b);
		border.x = l;
		border.y = t;
		border.z = r;
		border.w = b;
		return true;
	}

	return false;
}
